package com.jmu.medicineknows.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmu.medicineknows.dto.DrugPageDto;
import com.jmu.medicineknows.entity.Result;
import com.jmu.medicineknows.entity.SaleRecord;
import com.jmu.medicineknows.entity.ShoppingCart;
import com.jmu.medicineknows.mapper.ShoppingMapper;
import com.jmu.medicineknows.service.SaleRecordService;
import com.jmu.medicineknows.service.ShoppingCartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    ShoppingMapper shoppingMapper;
    @Resource
    SaleRecordService service;
    @Override
    public Result getList(DrugPageDto pageDto) {
        Page<ShoppingCart> shoppingCartPage =new Page<>(pageDto.getCurrentPage(),pageDto.getPageSize());
        LambdaQueryWrapper<ShoppingCart> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUid,pageDto.getSecurityNum());
//        if(pageDto.getFlag()=='1'){
//            wrapper.like(ShoppingCart::getName,pageDto.getContent());
//        }else if(pageDto.getFlag()=='2'){
//            wrapper.like(SaleRecord::getCode,pageDto.getContent());
//        }else {
//            wrapper.like(SaleRecord::getNumber,pageDto.getContent());
//        }
        //wrapper.between(SaleRecord::getDate, pageDto.start,pageDto.end);

//        wrapper.eq(SaleRecord::getCode,"236703");
        IPage<ShoppingCart> shoppingCartIPage = shoppingMapper.selectPage(shoppingCartPage,wrapper);
        return Result.defaultSuccess(shoppingCartIPage);

    }
    @Override
    public Result addToCart(ShoppingCart shoppingCart) {
        if ("".equals(shoppingCart.getUid())){
            return Result.failure(400,"医保卡号错误！");
        }
        LambdaQueryWrapper<ShoppingCart> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUid,shoppingCart.getUid()).eq(ShoppingCart::getName,shoppingCart.getName());
        System.out.println(shoppingMapper.selectCount(wrapper));
        int count=0;
        if(shoppingMapper.selectCount(wrapper)==0){
            return Result.defaultSuccess( shoppingMapper.insert(shoppingCart));
        }else{
            ShoppingCart shoppingCart1=shoppingMapper.selectOne(wrapper);
            shoppingCart.setNumber(shoppingCart.getNumber()+shoppingCart1.getNumber());
            shoppingCart.setTotal(shoppingCart.getTotal()+shoppingCart1.getTotal());
            return Result.defaultSuccess( shoppingMapper.update(shoppingCart, wrapper));
        }

    }

    @Override
    public Result updateCart(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> wrapper =new LambdaQueryWrapper();
        System.out.println(shoppingCart);
        shoppingCart.setTotal(shoppingCart.getNumber()*shoppingCart.getPrice());
        wrapper.eq(ShoppingCart::getId,shoppingCart.getId());
        return Result.defaultSuccess(shoppingMapper.update(shoppingCart,wrapper));
    }

    @Override
    public Result delete(List<Integer> list) {
        if(!list.isEmpty()){
            return Result.defaultSuccess(shoppingMapper.deleteBatchIds(list));
        }{
            return Result.defaultFailure();
        }
    }

    @Override
    public Result order(List<ShoppingCart> shoppingCarts) {
        //int count=0;
        //List<Result> list =new ArrayList<>();

        Result result =Result.newInstance();
        for(ShoppingCart shoppingCart:shoppingCarts){
            SaleRecord saleRecord=new SaleRecord();
            saleRecord.setCode(shoppingCart.getDid());
            saleRecord.setNumber(shoppingCart.getUid());
            saleRecord.setTotal(shoppingCart.getNumber());
            saleRecord.setReceivable(shoppingCart.getTotal());
            saleRecord.setName(shoppingCart.getName());
            result =(service.orderAdd(saleRecord));
        }

        return result;
    }

}
